﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class cms_default : System.Web.UI.Page
{

    debioscoopEntities context = new debioscoopEntities();

    protected void gvAccount_SelectedIndexChanged(object sender, EventArgs e)
    {
        dvAccount.ChangeMode(DetailsViewMode.ReadOnly);
        dvAccount.Visible = true;
    }

    protected void dvAccount_PreRender(object sender, EventArgs e)
    {
        //if (dvAccount.DataItemCount == 0)
        //{
        //    dvAccount.DataSource = dsDetailAccount;
        //    dvAccount.ChangeMode(DetailsViewMode.Insert);
        //}
        //else
        //{
        //    dvAccount.DataSource = dsSelectAccount;
        //}
    }

    protected void tbUsername_TextChanged(object sender, EventArgs e)
    {
        
        var query = from a in context.accounts
                    select a;
        bool found = false;
        String gebruikersnaam = (gvAccount.Controls[0].Controls[0].FindControl("tbUsername") as TextBox).Text;

        foreach (var a in query.ToList())
        {

            if (a.gebruikersnaam.Equals(gebruikersnaam))
                found = true;
        }
        if (found)
            (gvAccount.Controls[0].Controls[0].FindControl("lblUsername") as Label).Text = "Sorry, uw gewenste gebruikersnaam is al in gebruik";
        else
            (gvAccount.Controls[0].Controls[0].FindControl("lblUsername") as Label).Text = "Gebruikersnaam is goedgekeurt";
            //lblGebruikersnaam.Text = "Gebruikersnaam is goedgekeurt";

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if ((gvAccount.Controls[0].Controls[0].FindControl("lblUsername") as Label).Text == "Gebruikersnaam is goedgekeurt")
            {
                var a = context.accounts.Create();
                a.voornaam = (gvAccount.Controls[0].Controls[0].FindControl("tbFirstName") as TextBox).Text;
                a.tussenvoegsel = (gvAccount.Controls[0].Controls[0].FindControl("tbTussenvoegsel") as TextBox).Text;
                a.achternaam = (gvAccount.Controls[0].Controls[0].FindControl("tbSurName") as TextBox).Text;
                a.gebruikersnaam = (gvAccount.Controls[0].Controls[0].FindControl("tbUsername") as TextBox).Text;
                a.wachtwoord = (gvAccount.Controls[0].Controls[0].FindControl("tbPassword") as TextBox).Text;
                a.geboortedatum = DateTime.ParseExact((gvAccount.Controls[0].Controls[0].FindControl("tbDateOfBirth") as TextBox).Text, "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                a.straat = (gvAccount.Controls[0].Controls[0].FindControl("tbStreet") as TextBox).Text;
                a.huisnummer = (gvAccount.Controls[0].Controls[0].FindControl("tbStreetNumber") as TextBox).Text;
                a.postcode = (gvAccount.Controls[0].Controls[0].FindControl("tbPostal") as TextBox).Text;
                a.geslacht = (gvAccount.Controls[0].Controls[0].FindControl("ddlSex") as DropDownList).Text;
                //a.taal = (gvAccount.Controls[0].Controls[0].FindControl("ddlLanguage") as DropDownList).Text;
                if ((gvAccount.Controls[0].Controls[0].FindControl("ddlLanguage") as DropDownList).Text.Equals("english"))
                {
                    a.taal = "EN";
                }
                else if ((gvAccount.Controls[0].Controls[0].FindControl("ddlLanguage") as DropDownList).Text.Equals("dutch"))
                {
                    a.taal = "NL";
                }
                if ((gvAccount.Controls[0].Controls[0].FindControl("ddlNewsletter") as DropDownList).Text.Equals("ja"))
                {
                    a.nieuwsbrief = true;
                }
                else
                {
                    a.nieuwsbrief = false;
                }
                String strSubscription = (gvAccount.Controls[0].Controls[0].FindControl("ddlSubscription") as DropDownList).Text;
                var sub = context.abbonements.Where(s => s.abbonementnaam == strSubscription).First();
                a.abbonement = sub;

                context.accounts.Add(a);
                context.SaveChanges();
                Response.Redirect(Request.RawUrl);
            }
        }
        catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
        {
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                }
            }
        }


    }
    protected void dvAccount_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
}